package until;

import config.commonconfig;
import io.restassured.RestAssured;
import io.restassured.response.Response;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import static io.restassured.RestAssured.given;
import static io.restassured.config.DecoderConfig.decoderConfig;

/**
 * @Description
 * @auther lileilei
 * @create 2020-01-04 13:45
 */
public class RequestEncapsulation {

    public Map<String, Object> getmethod(Map<String, Object> map, String url) {
        RestAssured.baseURI = commonconfig.url;
        Response response;
        if (map == null) {
            response = given()
                    .contentType("application/json;charset=UTF-8")
                    .request()
                    .get(url);
        } else {
            response = given().params(map)
                    .contentType("application/json;charset=UTF-8")
                    .request()
                    .get(url);
        }

        Integer code = response.statusCode();
        Long time = response.time();
        Map<String, Object> mapone = new HashMap<>();
        mapone.put("time", time);
        mapone.put("code", code);
        mapone.put("reslut", response.asString());
        return mapone;
    }

    public Map<String, Object> postmethod(Map<String, Object> map, String url) {
        RestAssured.baseURI = commonconfig.url;
        Response response;
        if (map == null) {
            response = given()
                    .contentType("application/json;charset=UTF-8")
                    .request()
                    .post(url);
        } else {
            response = given().params(map)
                    .contentType("application/json;charset=UTF-8")
                    .request()
                    .post(url);
        }

        Integer code = response.statusCode();
        Long time = response.time();
        Map<String, Object> mapone = new HashMap<>();
        mapone.put("time", time);
        mapone.put("code", code);
        mapone.put("reslut", response.asString());
        return mapone;
    }

    public Map<String, Object> putmethod(Map<String, Object> map, String url) {
        RestAssured.baseURI = commonconfig.url;
        Response response;
        if (map == null) {
            response = given()
                    .contentType("application/json;charset=UTF-8")
                    .request()
                    .put(url);
        } else {
            response = given().params(map)
                    .contentType("application/json;charset=UTF-8")
                    .request()
                    .put(url);
        }
        Integer code = response.statusCode();
        Long time = response.time();
        Map<String, Object> mapone = new HashMap<>();
        mapone.put("time", time);
        mapone.put("code", code);
        mapone.put("reslut", response.asString());
        return mapone;
    }

    public Map<String, Object> deletemethod(Map<String, Object> map, String url) {
        RestAssured.baseURI = commonconfig.url;
        Response response;
        if (map == null) {
            response = given()
                    .contentType("application/json;charset=UTF-8")
                    .request()
                    .delete(url);
        } else {
            response = given().params(map)
                    .contentType("application/json;charset=UTF-8")
                    .request()
                    .delete(url);
        }
        Integer code = response.statusCode();
        Long time = response.time();
        Map<String, Object> mapone = new HashMap<>();
        mapone.put("time", time);
        mapone.put("code", code);
        mapone.put("reslut", response.asString());
        return mapone;
    }

    public Map<String, Object> getsendfile(Map<String, Object> map, String url) {
        RestAssured.config = RestAssured.config().decoderConfig(decoderConfig().defaultContentCharset("UTF-8"));
        File f = new File(map.get("multipartFile").toString());
        try {
            InputStream is = new FileInputStream(f);
            String fileName = f.getName();
            Response response = given()
                    .contentType("multipart/form-data")
                    .multiPart("file", fileName, is)
                    .params(map)
                    .request()
                    .post(url);
            Integer code = response.statusCode();
            Long time = response.time();
            Map<String, Object> mapone = new HashMap<>();
            mapone.put("time", time);
            mapone.put("code", code);
            mapone.put("reslut", response.asString());
            return mapone;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }

    }
}
